Page({
  request: {},
  data: {
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.request = require('../../common/networks');
    this.util = require('../../common/util');
    this.setData({
      keyword: options.keyword
    });
    this.search(options.keyword);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.setNavigationBarTitle({
      title: '蒲公英家居-查询订单',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},
  bindSearch: function(e) {
    var keyword = this.data.keyword;
    this.search(keyword);
  },
  bindKeywordInput: function(e) {
    this.data.keyword = e.detail.value;
  },
  search: function(keyword) {
    if (this.util.isEmpty(keyword)) {
      this.util.showToast("请输入你要查询的手机号码或者订单号");
      return;
    }
    var isMobile = this.util.isMobile(keyword);
    var isOrderNo = !isMobile && this.util.isOrderNo(keyword);
    if (!isMobile && !isOrderNo) {
      this.util.showToast("请输入正确的手机号码或者订单号");
      return;
    }

    var page = this;
    var params = {
      mobile: keyword
    }

    wx.showLoading({
      title: '查询中...',
      mask: true
    })

    var success = function(data) {
      wx.hideLoading();

      var orderList = !!data && !!data.length ? data : [];
      if (orderList.length == 0 && !!data) {
        orderList.push(data);
      }
      // orderList.push({});
      page.setData({
        orderList: orderList
      });
    }
    var params = {};
    if (isMobile) {
      params.mobile = keyword;
      this.request.searchByMobile(params, success);
    } else {
      params.orderNo = keyword;
      this.request.searchByOrderNo(params, success);
    }
  },
  bindOrderDetail: function(e) {
    var orderNo = e.currentTarget.dataset.orderNo;
    console.log(orderNo);
    wx.navigateTo({
      url: '../../pages/detail/detail?orderNo=' + orderNo,
    })
  }
})